package com.zx.platform.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-12-10 Time: 下午9:21 To
 * change this template use File | Settings | File Templates.
 */
public class DateUtils {

    private static final String DATE_TIME_FMT = "yyyy-MM-dd HH:mm:ss";

    private static final String DATE_FMT = "yyyy-MM-dd";

    private static final String TIME_FMT = "HH:mm:ss";
    /**
     * 时间戳格式化
     */
    public static final String TIMESTAMP_FOMART="yyyy-MM-dd HH:mm:ss.SSS";

    public static final String DATE_TIME_FMT_OTHER="yyyyMMddHHmmss";

    /**
     * 获得格式化日期
     * @param format
     * @return
     */
    public static Date formatDate(Date date, String format) {
        String dateStr = dateToString(date, format);
        return strToDate(dateStr, format);
    }

    /**
     * "yyyy-MM-dd HH:mm:ss"
     *
     * @param strDate
     * @param format
     * @return
     */
    public static Date strToDate(String strDate, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        ParsePosition pos = new ParsePosition(0);
        return formatter.parse(strDate, pos);
    }

    /**
     * "yyyy-MM-dd HH:mm:ss"
     *
     * @param date
     * @param format
     * @return
     */
    public static String dateToString(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * 获取日期
     *
     * @param date
     * @param tianshu
     * @return
     */
    public static Date addDate(Date date, int tianshu) {
        Date returnDate = new Date();
        long Time = (date.getTime() / 1000) + 24 * 60 * 60 * tianshu;
        returnDate.setTime(Time * 1000);
        return returnDate;
    }

    /**
     * 获取日期
     *
     * @param date
     * @param hour
     * @return
     */
    public static Date addDateByHour(Date date, int hour) {
        Date returnDate = new Date();
        long Time = (date.getTime() / 1000) + 60 * 60 * hour;
        returnDate.setTime(Time * 1000);
        return returnDate;
    }

    /**
     * 以分钟增加
     *
     * @param date
     * @param minute
     * @return
     */
    public static Date addDateByMinute(Date date, int minute) {
        Date returnDate = new Date();
        long Time = (date.getTime() / 1000) + 60 * minute;
        returnDate.setTime(Time * 1000);
        return returnDate;
    }

    /**
     * @return 日期的格式化器
     */
    private static SimpleDateFormat getDateFmtter() {
        return createDateFormat(DATE_FMT);
    }

    /**
     * @return 日期时间的格式化器
     */
    private static SimpleDateFormat getDateTimeFmtter() {
        return createDateFormat(DATE_TIME_FMT);
    }

    public static SimpleDateFormat createDateFormat(String format) {
        return new SimpleDateFormat(format);//NOPMD
    }

    /**
     * 功能：获取某个日期当月的第一天
     *
     * @param date
     * @return
     */
    public static Date getMonthFirstDateOf(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        calendar.add(Calendar.DATE, day * (-1) + 1);
        return calendar.getTime();
    }

    /**
     * 功能：获取某个日期的月份
     *
     * @param date
     * @return
     */
    public static int getMonthOf(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 功能：获取某个日期当年的第一天
     *
     * @param date
     * @return
     */
    public static Date getYearFirstDateOf(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, 1);
        calendar.set(Calendar.DATE, 1);
        return calendar.getTime();
    }

    /**
     * 功能：获取某个日期的年份
     *
     * @param date
     * @return
     */
    public static int getYearOf(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 将字符串转换成java.sql.Date
     *
     * @param dateStr 如果为"" 或 null ,默认为 yyyy-MM-dd
     * @return
     * @throws ParseException
     */
    public static java.sql.Date parseSqlDate(String dateStr)
            throws ParseException {
        Date date = parseUtilDate(dateStr);
        return (date == null) ? null : (new java.sql.Date(date.getTime()));
    }

    /**
     * 将字符串转换成java.util.Date,如果字符串是yyyy-MM-dd则解析成只有日期部分的日期, <br>
     * 如果是yyyy-MM-dd HH:mm:ss,则解析成日期时间型日期
     *
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public static Date parseUtilDate(String dateStr) throws ParseException {
        if (dateStr == null || dateStr == "") {
            return null;
        } else if (dateStr.length() <= 10) {
            return getDateFmtter().parse(dateStr);
        } else {
            return getDateTimeFmtter().parse(dateStr);
        }
    }



    /**
     * 根据传入的开始季度，结束季度，获取中间的季度列表。季度格式：201301,201302等
     *
     * @param beginSeason
     * @param endSeason
     * @return
     * @throws ParseException
     */
    public static List getSeasonList(String beginSeason, String endSeason) throws ParseException {
        if (beginSeason.length() != 6 || endSeason.length() != 6) {
            return null;
        }

        int beginYear = Integer.parseInt(beginSeason.substring(0, 4));
        int beginNum = Integer.parseInt(beginSeason.substring(5));
        int beginS = Integer.parseInt(beginSeason);
        int endS = Integer.parseInt(endSeason);
        if (beginS > endS) {
            return null;
        }
        List list = new ArrayList();
        if (beginS == endS) {
            list.add(beginSeason);
            return list;
        }
        int i = 0;//加一个参数，防止异常导致内存溢出
        while (!(beginYear + "0" + beginNum).equals(endSeason)) {
            list.add(beginYear + "0" + beginNum);
            if (beginNum > 4) {//期次异常
                break;
            } else if (beginNum == 4) {
                beginYear++;
                beginNum = 1;
            } else {
                beginNum++;
            }
            i++;
        }
        list.add(endSeason);
        return list;
    }

    /**
     * 根据传入的开始季度，结束季度，获取中间的季度列表。季度格式：201301,201302等
     * 返回季度格式为：{'11-4季','12-1季','12-2季'}
     *
     * @param beginSeason
     * @param endSeason
     * @return
     * @throws ParseException
     */
    public static List getSeasonList1(String beginSeason, String endSeason) throws ParseException {
        if (beginSeason.length() != 6 || endSeason.length() != 6) {
            return null;
        }

        int beginYear = Integer.parseInt(beginSeason.substring(0, 4));
        int beginNum = Integer.parseInt(beginSeason.substring(5));
        int beginS = Integer.parseInt(beginSeason);
        int endS = Integer.parseInt(endSeason);
        if (beginS > endS) {
            return null;
        }
        List list = new ArrayList();
        if (beginS == endS) {
            list.add(beginSeason);
            return list;
        }
        int i = 0;//加一个参数，防止异常导致内存溢出
        while (!(beginYear + "0" + beginNum).equals(endSeason)) {
            list.add((beginYear + "").substring(2) + "-" + beginNum + "季");
            if (beginNum > 4) {//期次异常
                break;
            } else if (beginNum == 4) {
                beginYear++;
                beginNum = 1;
            } else {
                beginNum++;
            }
            i++;
        }
        list.add(endSeason.substring(2, 4) + "-" + endSeason.substring(5) + "季");
        return list;
    }
}
